package net.keebook.webapp.bean;

import java.io.IOException;
import java.util.TimeZone;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import net.keebook.webapp.dao.UserDao;
import net.keebook.webapp.entity.User;
import net.keebook.webapp.model.SessionModel;
import net.keebook.webapp.util.BCrypt;
import net.keebook.webapp.util.Messages;

import org.apache.log4j.Logger;


@ManagedBean
@RequestScoped
public class AddUserBean {
	private String userId;
	private String username;
	private String password;
	private Boolean passwordChecked = false;
	private Boolean admin;
	private Boolean confirmed = false;
	private Boolean enabled = true;
	private SessionModel sessionModel;
	
	@Inject
	private UserDao userDao;
	
	@PostConstruct
	public void init() throws IOException {
		setSessionModel(SessionModel.getInstance(FacesContext.getCurrentInstance()));
		
		if (!getSessionModel().getAdmin()) {
			Logger.getLogger(this.getClass()).warn("Forbidden access to bean from userId="+getSessionModel().getUserId());
			FacesContext.getCurrentInstance().getExternalContext().redirect("403.xhtml");
			return;
		}
		
		if (FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().containsKey("id")) {
			String id = new String(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"));
			User user = userDao.get(id);
			setUserId(user.getExposableId());
			setUsername(user.getUsername());
			setAdmin(user.getAdmin());
			setConfirmed(user.getConfirmed());
			setEnabled(user.getEnabled());
		}
	}
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public Boolean getAdmin() {
		return admin;
	}
	public void setAdmin(Boolean admin) {
		this.admin = admin;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	
	public void save() throws IOException {
		if (!getSessionModel().getAdmin())  {
			Logger.getLogger(this.getClass()).warn("Forbidden access to bean from userId="+getSessionModel().getUserId());
			FacesContext.getCurrentInstance().getExternalContext().redirect("403.xhtml");
			return;
		}
		
		User user = new User();
		user.setUsername(getUsername());
		user.setHashedPassword(BCrypt.hashpw(getPassword(), BCrypt.gensalt()));
		user.setAdmin(getAdmin());
		user.setTimezone(TimeZone.getDefault().getID());
		user.setEnabled(getEnabled());
		user.setConfirmed(getConfirmed());
		
		userDao.store(user);
		FacesContext.getCurrentInstance().getExternalContext().redirect("admin-edituser.xhtml?id="+user.getExposableId()+"&msg="+Messages.ITEM_CREATED);
	}
	
	public void update() throws IOException {
		if (!getSessionModel().getAdmin())  {
			Logger.getLogger(this.getClass()).warn("Forbidden access to bean from userId="+getSessionModel().getUserId());
			FacesContext.getCurrentInstance().getExternalContext().redirect("403.xhtml");
			return;
		}
		
		User user = userDao.get(getUserId());
		user.setUsername(getUsername());
		user.setAdmin(getAdmin());
		user.setEnabled(getEnabled());
		user.setConfirmed(getConfirmed());
		
		userDao.update(user);
		FacesContext.getCurrentInstance().getExternalContext().redirect("admin-edituser.xhtml?id="+user.getExposableId()+"&msg="+Messages.ITEM_UPDATED);
	}

	public SessionModel getSessionModel() {
		return sessionModel;
	}

	public void setSessionModel(SessionModel sessionModel) {
		this.sessionModel = sessionModel;
	}

	public Boolean getConfirmed() {
		return confirmed;
	}

	public void setConfirmed(Boolean confirmed) {
		this.confirmed = confirmed;
	}

	public Boolean getEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public Boolean getPasswordChecked() {
		return passwordChecked;
	}

	public void setPasswordChecked(Boolean passwordChecked) {
		this.passwordChecked = passwordChecked;
	}
}
